package com.itheima.controller;

import com.itheima.pojo.Book;
import com.itheima.service.BookService;
import com.itheima.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@RestController
@RequestMapping("books")
public class LoginService {
    @Autowired
    BookService bookService;
    @GetMapping("/{name}")
    public Object Login (@PathVariable String name, HttpServletRequest request){
        System.out.println("这是liyujia的测试代码");
        String limitIp = IpUtil.getIpAddress(request);
        System.out.println(name);
        if (!name.equals("")){
            List<Book> books = bookService.find();
            if (books!=null && books.size()>0)
            {
                boolean addtime = bookService.addtime(name,limitIp);
                return books.get(0).getName();
            }else {
                boolean addtime = bookService.addtime(name,limitIp);
                List<Book> books1 = bookService.find();
                return books1.get(0).getName();
            }

        }
        if (""==null){
            return null;
        }
       // List<Book> books = bookService.find();
        return name;
    }

}
